﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.SceneControl;
using Engine.Physic;
using Engine.SceneControl.BrutalForceWorld;
using Engine;
using Engine.Modelo;
using Engine.Modelo.Animation;
using Engine.Input;
using Engine.Commands;
using Engine.Features.SkyBox;
using Engine.Light;
using Engine.Behavior;
using Engine.Material.Shader;
using Engine.Physic.PhysicObjects.JigLibxObjects;
using Engine.SceneControl.Object;
using Engine.Material;
using Engine.Cameras;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Engine.MessageSystem;
using Engine.Utils.Atributes;
using Engine.SceneControl.HUD;
using Engine.Behaviors.Actions;
using Engine.Factories;
using Engine.Factories.FactoriesList.CharacterFactory;
using Engine.Factories.FactoriesList.ObjectFactory;
using Engine.Factories.FactoriesList.TerrainFactory;
using Engine.Events;
using Engine.Behaviors.Behavior;
using Engine.SceneControl.Scene;
using Microsoft.Xna.Framework.Graphics;
using Engine.SceneControl.Scene.DefRender;
using Engine.Light.Deferred;
using Engine.Features.PickingComponent;
using Engine.SceneControl.Scene.PostEffects;
using Engine.IA;
using Engine.IA.Waypoints;
using Engine.IA.FSM;
using Engine.Material.Deferred;
using Engine.IA.Steering;

namespace Etapa1.Screens
{
    [Services("Engine.EngineStuff", Escopo = Escope.REQUEST)]
    public class IslandScreen : SceneScreen
    {
        private JigLibxWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;
        private Manager manager;
        private WaypointHandler handler = new WaypointHandler();
        private ICamera cam;
        private BindKeyCommand bk1;
        WaterShader shader2;

        IObject obj = null;

        public IslandScreen(EngineStuff es)
        {
            this.engine = es;
            engine.BackGroundColor = Color.Sienna;
            physicWorld = engine.GetManagedObject<JigLibxWorld>("Engine.Physic.JigLibxWorld");
            physicWorld.isDebugDraw = true;
            mundo = new BrutalForceWorld(physicWorld);
            manager = engine.Manager;

        }

        public override void LoadContent(Engine.EngineStuff es)
        {
            //this.AddHud(new SaveConnectedHud(handler, mundo));
            es.ActivateDebugVectorForWorld(mundo);

            base.LoadContent(es);


            SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
            sm.LoadModelo(engine.CustomContentManager);
            IPhysicObject pi = new TriangleMeshObject(sm, Matrix.Identity, Vector3.Zero);
            pi.isMotionLess = true;
            IShader shader = new NormalDeferred();
            shader.Initialize(engine);
            IMaterial mat = new Material(shader);
            IObject obj4 = new SimpleObject(mat, pi, sm);
            mundo.AddObject(obj4);

            //SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario", "..\\Content\\Model\\islandmap");
            //sm.LoadModelo(engine.CustomContentManager);
            //IPhysicObject pi = new TriangleMeshObject(sm, Matrix.Identity, Vector3.Zero);
            //pi.isMotionLess = true;
            //IShader shader = new NormalDeferred();
            //shader.Initialize(engine);
            //IMaterial mat = new Material(shader);
            //IObject obj4 = new SimpleObject(mat, pi, sm);
            //mundo.AddObject(obj4);

            SimpleModel sm2 = new SimpleModel("..\\Content\\Model\\block");
            sm2.LoadModelo(engine.CustomContentManager);
            //IPhysicObject pi2 = new SphereObject(sm2, new Vector3(50, 10, 10), Matrix.Identity, 5);
            IPhysicObject pi2 = new BoxObject(sm2, Vector3.Zero, Matrix.Identity, Vector3.Zero);
            pi2.isMotionLess = true;
            shader2 = new WaterShader("..\\Content\\Textures\\cubemap");
            shader2.Position = new Vector3(-500,9, -500);
            //shader2.Scale = new Vector3(55, 1, 55);
            shader2.Initialize(engine);




            mat = new Material(shader2);
            obj = new SimpleObject(mat, pi2, sm2);
            mundo.AddObject(obj);

            //{
            //    SimpleModel smx2 = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\stone");
            //    smx2.LoadModelo(engine.CustomContentManager);
            //    //IPhysicObject pi2 = new SphereObject(sm2, new Vector3(50, 10, 10), Matrix.Identity, 5);
            //    IPhysicObject pix2 = new BoxObject(sm2, new Vector3(10, 10, 50), Matrix.Identity, new Vector3(1, 1, 1));
            //    //IPhysicObject pix2 = new TriangleMeshObject(sm2, Matrix.Identity, new Vector3(10, 10, 50));
            //    pix2.isMotionLess = true;
            //    IShader shaderx2 = new NormalDeferred();
            //    shaderx2.Initialize(engine);
            //    mat = new Material(shaderx2);
            //    obj = new SimpleObject(mat, pix2, smx2);
            //    mundo.AddObject(obj);
            //}

            //GroupedObjectsRequest gr = new GroupedObjectsRequest("..\\Content\\Model\\cenario", PhysicObjectTypes.TRIANGLEMESHOBJECT,new Vector3(450,65,450), false, ShaderLib.TEXTURE);
            //List<IObject> list = manager.getObject(gr) as List<IObject>;

            //foreach (IObject item in list)
            //{
            //    mundo.AddObject(item);
            //}


            





            LightDirectional ld = new LightDirectional(new Vector3(0, -10, 50), Color.White);
            LightDirectional ld2 = new LightDirectional(new Vector3(30, -50, 30), Color.White);
            //mundo.AddLight(ld);
            mundo.AddLight(ld2);

            LightPoint lp1 = new LightPoint(new Vector3(450,50,450), Color.Red, 100, 10);
            //mundo.AddLight(lp1);

            SpotLight sp1 = new SpotLight(new Vector3(450), new Vector3(-1, -1, -1), 1, 300, Color.Green, (float)Math.Cos(Math.PI / 3), 100f);
            //mundo.AddLight(sp1);

            cam = new CameraPrimeiraPessoa(engine.GraphicsDevice.Viewport, true);
            cam.FarPlane = 4000;


            ////Criacao de uma Tecnica
            BasicDeferredRenderTechnic dr = new BasicDeferredRenderTechnic(engine);
            //dr.isPhysicDebug = true;
            //dr.isDebug = false;
            dr.DeferredLightMap = new ShadowLightMap();  
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;

            mundo.AddCamera(cam);
            this.World = mundo;

            SkyBoxUpdateParametersCommand sbc = new SkyBoxUpdateParametersCommand(cam);
            sbc.setStyle(Skybox.SkyType.SUNNY);
            engine.AddComponent(sbc);





        }


        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {


            if (Keyboard.GetState().IsKeyDown(Keys.D1))
                shader2.WaterAmount = 0;

            if (Keyboard.GetState().IsKeyDown(Keys.D2))
            {
                shader2.WaterAmount = 1;
                shader2.ShallowWaterColor = Color.DarkSeaGreen;
                shader2.DeepWaterColor = Color.Navy;
                shader2.ReflectionColor = Color.DarkGray;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D3))
            {
                shader2.WaterAmount = 1;
                shader2.ShallowWaterColor = Color.Gold;
                shader2.DeepWaterColor = Color.Red;
                shader2.ReflectionColor = Color.White;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D4))
            {
                shader2.WaveFrequency = .5f;
                shader2.WaveAmplitude = .3f;
                shader2.BumpHeight = 1f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D5))
            {
                shader2.WaveFrequency = .1f;
                shader2.WaveAmplitude = .1f;
                shader2.BumpHeight = .1f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D6))
            {
                shader2.WaveFrequency = .1f;
                shader2.WaveAmplitude = .5f;
                shader2.BumpHeight = .1f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D7))
            {
                shader2.WaveFrequency = .1f;
                shader2.WaveAmplitude = 2f;
                shader2.BumpHeight = 1f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D8))
            {
                shader2.WaveFrequency = 0;
                shader2.WaveAmplitude = 0;
                shader2.BumpHeight = 0;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.D9))
            {
                shader2.WaveFrequency = 0;
                shader2.WaveAmplitude = 0;
                shader2.BumpHeight = 1;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D0))
                shader2.SetDefault();

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }



    }
}
